/*
 * Copyright (c) 2006-2023, xxxx Development Team
 *
 *
 * Change Logs:
 * Date           Author       Version      Notes
 * 2022-11-19     TSL          v0.1         first version
 */
/* 头文件 ---------------------------------------------------------------------*/
#include "main.h"
#include "module_led.h"

/* 宏定义 ---------------------------------------------------------------------*/
#define LED_DEV_INIT_AND_REGISTER(_ID_) do {        \
    fsl_ioctl_init_dev(&dev_led[_ID_]);             \
    dev_led[_ID_].on = led##_ID_##_on;              \
    dev_led[_ID_].off = led##_ID_##_off;            \
    fsl_ioctl_register_dev(&dev_led[_ID_]);         \
} while(0)

/* 类型定义 -------------------------------------------------------------------*/

/* 函数声明 -------------------------------------------------------------------*/
static void led_init(void);
static void led_setup(uint8_t index, uint16_t on_time_ms,uint16_t off_time_ms,uint8_t repeat_num);
static uint8_t led_is_busy(uint8_t index);

// 点亮和熄灭硬件接口（根据实际实例数量修改）
static void led0_on(void);
static void led1_on(void);
static void led2_on(void);
static void led0_off(void);
static void led1_off(void);
static void led2_off(void);

/* 变量声明 -------------------------------------------------------------------*/
static ioctl_dev_t dev_led[LED_NUM_MAX];

const struct led_ops api_led = {
    .init = led_init,
    .setup = led_setup,
    .is_busy = led_is_busy,
};

/*
 * 函数说明
 */
static void led_init(void)
{
    // LED引脚硬件GPIO初始化
    GPIO_InitTypeDef  GPIO_InitStructure;
    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE); 
    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

    GPIO_InitStructure.GPIO_Pin  =  GPIO_Pin_2;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_ResetBits(GPIOA,GPIO_Pin_2);

    GPIO_InitStructure.GPIO_Pin  =  GPIO_Pin_15;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_ResetBits(GPIOA,GPIO_Pin_15);

    GPIO_InitStructure.GPIO_Pin  =  GPIO_Pin_1;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_ResetBits(GPIOA,GPIO_Pin_1);
    
    // LED实例初始化和设备注册
    LED_DEV_INIT_AND_REGISTER(0);
    LED_DEV_INIT_AND_REGISTER(1);
    LED_DEV_INIT_AND_REGISTER(2);
}

/*
 * 函数说明
 */
static void led_setup(uint8_t index, uint16_t on_time_ms,uint16_t off_time_ms,uint8_t repeat_num)
{
    // 关键参数合法性检查
    if (index >= LED_NUM_MAX) return;

    fsl_ioctl_setup(&dev_led[index], on_time_ms, off_time_ms, repeat_num);
}

/*
 * 函数说明
 */
static uint8_t led_is_busy(uint8_t index)
{
    // 关键参数合法性检查
    if (index >= LED_NUM_MAX) return 0;

    return fsl_ioctl_is_busy(&dev_led[index]);
}

/*
 * 函数说明
 */
static void led0_on(void)
{
    GPIO_SetBits(GPIOA, GPIO_Pin_2);
}
static void led1_on(void)
{
    GPIO_SetBits(GPIOA, GPIO_Pin_15);
}
static void led2_on(void)
{
    GPIO_SetBits(GPIOA, GPIO_Pin_1);
}
static void led0_off(void)
{
    GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}
static void led1_off(void)
{
    GPIO_ResetBits(GPIOA,GPIO_Pin_15);
}
static void led2_off(void)
{
    GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}
